<script>
  import { showToast } from 'vant'
  import events from "@/utils/events";
  import count from "./count";
  import { defineComponent } from 'vue'
  const component = defineComponent({
    self:null,
    props:['hidePrompt'],
    data(){
      count.addTime()
      return {
        uniqueKey:count.getTime(),
        validateList:[]
      }
    },
    created() {
      events.subscribe(this.uniqueKey+'-item',(data)=>{
        this.validateList.push(data)
      })
    },
    methods:{
      async validate(formName){
        let message = "";
        let validateList = this.validateList;
        for(let i = 0;i<validateList.length;i++){
          let item = validateList[i]
          if ((formName!==undefined && formName.indexOf(item.formName)!==-1) || formName === undefined){
            if(message ? false : message = await item.validate()){
              break;
            }
          }
        }
        if(!this.hidePrompt && message){
          showToast(message);
        }
        return message
      }
    }
  })
  export default component
</script>

<template>
   <div>
     <slot></slot>
   </div>
</template>

<style scoped>

</style>
